#!/bin/sh
#

# Ligne complète pour pandoc:
# pandoc --template=lib/template.html -c lib/pandoc.css -H lib/head.html -B lib/header.html -A lib/footer.html "$mdfile" -o "$htmlfile"

MARKDOWN_FILES=$(find src -name \*.md)
ASCIIDOC_FILES=$(find src -name \*.txt)

for mdfile in $MARKDOWN_FILES; do
  htmlfile="${mdfile%%.*}.html"
  if [ ! -f "$htmlfile" ] || [ "$mdfile" != "$htmlfile" ]; then
    echo "--> generate with pandoc '$htmlfile' from '$mdfile'"
    pandoc --template=lib/template.html -c lib/pandoc.css "$mdfile" -o "$htmlfile"
  fi
done

for mdfile in $ASCIIDOC_FILES; do
  htmlfile="${mdfile%%.*}.html"
  if [ ! -f "$htmlfile" ] || [ "$mdfile" != "$htmlfile" ]; then
    echo "--> generate with asciidoc'$htmlfile' from '$mdfile'"
    asciidoc -o "$htmlfile" -b html5 "$mdfile" 
  fi
done

echo "--> copy html files to public dir"
# HTML_FILES=$(find src -name \*.html)
# cp --parents src/*.html public
(cd src && cp -r --parent . ../public)
# (cd src && find . -name \*.html -exec cp --parents {} ../public \;)

# Build rss ?

echo "coucou ?"

LAST_CHANGES=$(git log --pretty="format:" --name-only |  grep  "src/posts/20*" | grep ".html" | uniq | tac | head -10)
echo "liste des 10 derniers fichiers qui ont changés : $LAST_CHANGES"


ITEMS=""
for change in $LAST_CHANGES
do
  echo $change
  titre=$(awk -F"<h1>|</h1>" '{for(i=2;i<=NF;i+=2){print $i}}' RS="" $change)
  lien=https://elsif.fr/$(echo $change | awk  '{gsub(/src/, ""); print $1}')
  updated=$(date -Rr $change)
  summary=$(awk -F"<article>|</article>" '{for(i=2;i<=NF;i+=2){print $i}}' RS="" $change)

  ITEMS="$ITEMS \n <entry xml:lang='fr'><title>$titre</title><link href='$lien' rel='alternate' type='text/html'/><upadated>$updated</updated><id>$(shasum $change | awk -F ' ' '{print $1}')</id><summary type='HTML'>$summary</summary></entry>"
done


cat << EOF > last_changes.atom.xml
<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Elsif.fr - Yannick François</title>
  <subtitle>les dernières mise à jour du site</subtitle>
  <link href="http://elsif.fr/atom.xml" rel="self" />
  <link href="http://elsif.fr/" />
  <id>elsif.fr</id>
  <updated>$(date -R)</updated>

  <entry xml:lang="fr">
    <title>TITRE</title>
    <link href="http://example.org/2003/12/13/atom03" />
    <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
    <link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>This is the entry content.</p>
      </div>
    </content>
    <author>
      <name>John Doe</name>
      <email>johndoe@example.com</email>
    </author>
  </entry>

$ITEMS
</feed>
EOF

ITEMS=""
for change in $LAST_CHANGES
do
  titre=$(awk -F"<h1>|</h1>" '{for(i=2;i<=NF;i+=2){print $i}}' RS="" $change)
  lien=https://elsif.fr/$(echo $change | awk  '{gsub(/\/src/, ""); print $1}')

  ITEMS="$ITEMS\n <item><title>$titre</title><link>$lien</link></item>"
done


cat << EOF > last_changes.rss
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Yannick François - elsif.fr</title>
    <description>Les derniers changements sur le site de Yannick François : elsif.fr</description>
    <link>http://elsif.fr/</link>
    <generator>panash</generator>
    <language>fr-FR</language>
    <lastBuildDate>$(date -R)</lastBuildDate>

    <item>
      <title>Notes du 5 Septembre 2018</title>
      <link>http://elsif.fr/posts/2018-09-05/</link>
      <pubDate>Wed, 05 Sep 2018 12:35:34 +0200</pubDate>

      <guid>http://elsif.fr/posts/2018-09-05/</guid>
      <description>Est-ce que j&amp;rsquo;ai vraiment besoin de créer un lieu en partant de rien ?
        Je pourrais rejoindre des associations du libre pour partager un lieu (et leur apporter de la pédagogie émancipatrice). Je pourrais aussi rejoindre des association de l&amp;rsquo;éducation populaire pour partager un lieu (et leur apporte de l&amp;rsquo;informatique). Une dernière option pourrait être de rejoindre une institution dont le lieu est mort ou vide.  Un moyen de pauer le loyer : monter une asso, faire cotiser les gens de l&amp;rsquo;association ?</description>
    </item>
  done

$ITEMS
</channel>
</rss>
EOF

echo "après fichier"

exit 0
